﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using SmartParing.IService;
using SmartParking.Base;
using SmartParking.Entities;
using SmartParking.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Unity;

namespace SmartParking.ViewModels.Pages
{
   public class MenuManagementViewModel:ViewModelBase
    {
        // public string PageTitle { get; set; } = "菜单数据维护";
        //public bool IsCanClose { get; set; } = true;

        //   public DelegateCommand CloseCommand { get; set; }
        //  IRegionManager _regionManager;

        //public DelegateCommand<string> RefreshCommand { get; set; }
        //public DelegateCommand<MenuItemModel> ModifyCommand { get; set; }
        //public DelegateCommand<MenuItemModel> DeleteCommand { get; set; }



        IMenuService _menuService;
        IDialogService _dialogService;
        IEventAggregator _eventAggregator; //事件总线
        private ObservableCollection<MenuItemModel> _menus =
          new ObservableCollection<MenuItemModel>();
        public MenuManagementViewModel(
           IRegionManager regionManager, IEventAggregator eventAggregator,
           IMenuService menuService, IDialogService dialogService):base(regionManager)
        {
            PageTitle = "菜单数据维护";
          //  _regionManager = regionManager;
            _menuService = menuService;
            _dialogService = dialogService;
            _eventAggregator = eventAggregator;
            //  CloseCommand = new DelegateCommand(DoClose);
            //RefreshCommand = new DelegateCommand<string>(Refresh);
            //ModifyCommand = new DelegateCommand<MenuItemModel>(DoModify);
            //DeleteCommand = new DelegateCommand<MenuItemModel>(DoDelete);


            Refresh();
        }

        public ObservableCollection<MenuItemModel> Menus { get; set; } =
           new ObservableCollection<MenuItemModel>();
        List<Entities.SysMenu> origMenus;
        string _search_key = "";

        public override void Refresh()
        {
           // _search_key = search_key;
            Menus.Clear();

            
            origMenus = _menuService.GetMenuList(SearchKey).ToList();
            MenuHelper.FillMenus(Menus, null,origMenus);
        }

       

        //private void DoClose()
        //{
        //    //拿到主区域，从区域里移除对应的页面，根据页面的名称
        //    var region = _regionManager.Regions["MainRegion"];
        //    var view = region.Views.Where(v => v.GetType().Name == "MenuManagementView").FirstOrDefault();
        //    if (view != null)
        //    {
        //        region.Remove(view);
        //    }
        //}

        public override void DoModify(object model)
        {
            DialogParameters ps = new DialogParameters();
            ps.Add("model", model);
            _dialogService.ShowDialog("ModifyMenuView", ps, result =>
            {
                // 判断子窗口的返回状态，如果OK，刷新当前页面，否则不管
                if (result.Result == ButtonResult.OK)
                {
                    this.Refresh();

                    //_eventAggregator.GetEvent<RefreshMenuEvent>()
                    //   .Publish();

                    _eventAggregator.GetEvent<RefreshMenuEvent>()
                        .Publish();
                }
            });
        }
        public override void DoDelete(object model)
        {
            try
            {
                if (MessageBox.Show("是否确定删除此项？", "提示", MessageBoxButton.YesNo) ==
                    MessageBoxResult.Yes)
                {
                    // 物理删除
                    _menuService.Delete<SysMenu>((model as MenuItemModel).MenuId.Value);

                    // 逻辑删除
                    // 通过特定字段进行数据过滤
                    MessageBox.Show("删除完成！", "提示");

                    this.Refresh();

                    _eventAggregator.GetEvent<RefreshMenuEvent>()
                      .Publish();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }

    }
}
